$(function() {
	$.get("dashboard/xhrGetListings", function(data) {
		
		for (var i = 0; i < data.length; i++) {
  			var str = "<div>" + data[i].productname + '<a class="del" rel="' + data[i].productid + '" href="#">Delete</a>' + "</div>";
			$("#list_inserts").append(str);
		}
		$('body').on('click', '.del', function() {
			delItem = $(this);
			var id = $(this).attr("rel");
			$.post("dashboard/xhrDeleteListings", {'id': id}, function(out) {
				if (out > 0) {
					delItem.parent().remove();
				}
			}, "json");
			return false;
		});
	}, "json");
	$("#random_insert").submit(function() {
		var url = $(this).attr('action');
		var data = $(this).serialize();
		
		$.post(url, data, function(data) {
			
			var str = "<div>" + data.productname + '<a class="del" rel="' + data.productid + '" href="#">Delete</a>' + "</div>";
			$("#list_inserts").append(str);
			
		}, "json");
		
		return false;
	});
	
});
